package org.gitflow.utils;

import java.sql.*;

/**
 * 数据库连接
 */
public class DBUtil {
    private static final String USERNAME = "sa";
    private static final String PASSWORD = "123456";
    private static final String URL = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=";
    private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    static {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL,USERNAME,PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static void close(ResultSet rs,Statement stmt,Connection conn){
        if (rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                System.out.println("关闭失败");
                e.printStackTrace();
            }
        }

        if (stmt != null){
            try {
                stmt.close();
            } catch (SQLException e) {
                System.out.println("关闭失败");
                e.printStackTrace();
            }
        }

        if (conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                System.out.println("关闭连接失败");
                e.printStackTrace();
            }
        }

    }

}
